#_*_coding:utf-8_*_
from django.shortcuts import render,HttpResponse,HttpResponseRedirect,render_to_response
from django.contrib import auth
from django.core.exceptions import ObjectDoesNotExist
# Create your views here.
import django
from xbman_cmdb import chart_output
from django.contrib.auth.decorators import login_required

@login_required
def index(request):
    data = chart_output.engine_room()
    status = chart_output.Machine_status()
    Business = chart_output.Business_Line()

    #return render_to_response('index.html',{'data':data,'status':status,'Business':Business})
    return render(request,'index.html',{'data':data,'status':status,'Business':Business})



def acc_login(request):
    if request.method == "POST":

        username = request.POST.get('email')
        password = request.POST.get('password')
        user = auth.authenticate(username=username,password=password)
        if user is not None:
            if user.valid_end_time: #设置了end time
                if django.utils.timezone.now() > user.valid_begin_time and django.utils.timezone.now()  < user.valid_end_time:
                    auth.login(request,user)
                    request.session.set_expiry(60*30)
                    #print 'session expires at :',request.session.get_expiry_date()
                    return HttpResponseRedirect('/')
                else:
                    return render(request,'login.html',{'login_err': 'User account is expired,please contact your IT guy for this!'})
            elif django.utils.timezone.now() > user.valid_begin_time:
                    auth.login(request,user)
                    request.session.set_expiry(60*30)
                    return HttpResponseRedirect('/')

        else:
            return render(request,'login.html',{'login_err': '您输入的用户名或密码错误,请重新输入！'})
    else:
        return render(request, 'login.html')

@login_required
def logout(request):
    auth.logout(request)
    return HttpResponseRedirect("/login/")